package com.hongdee.crm.mainReportForm.service.impl;

import com.hongdee.common.util.DateUtils;
import com.hongdee.common.util.TokenUtil;
import com.hongdee.crm.mainReportForm.dao.ImainReportFormDao;
import com.hongdee.crm.mainReportForm.service.IMainReportFormService;
import javafx.scene.input.TouchEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.hongdee.common.util.DateUtils.*;

/**
 * 名  称:MainReportFormService
 * 类  型: class
 * 描  述:
 * 路  径:com.hongdee.crm.mainReportForm.service.impl;
 * 作  者: 术洪超
 * 日  期:2017-04-07 09:15
 */
@Service
public class MainReportFormServiceImpl implements IMainReportFormService {

    @Autowired
    private ImainReportFormDao imainReportFormDao;


    /**
     * @param uid 登录用户 ID
     * @return ResultEntity
     * @throws Exception
     * @desc 查询产值和进厂辆次
     * @author 术洪超
     */
    @Override
    public List realCount(String uid) throws Exception {
        String companyId = TokenUtil.getCompanyId(uid);
        //今天日期
        String toDayS = DateUtils.getTimeStr("yyyy-MM-dd");
        long toDayL = DateUtils.parseTime(toDayS + " 24:00:00", "yyyy-MM-dd HH:mm:ss");
        //昨天日期
        long preDayL = DateUtils.addDayLongTime(toDayL, -2);
        String preDayS = DateUtils.formatLongTime(preDayL, "yyyy-MM-dd");

        //如果今天日期为1号，取上一月数据，1号之后取当月数据

        String preMotnS="";
        if(toDayS.equals(DateUtils.getMonthFirstDay(toDayS,"yyyy-MM-dd"))){
            toDayS = DateUtils.getPreMonthLastDay("yyyy-MM-dd");
            preMotnS = DateUtils.getPreMonthFirstDay("yyyy-MM-dd");
        }else{
            preMotnS = DateUtils.getMonthFirstDay(toDayS,"yyyy-MM-dd");
        }



        // 计算月产值
        List monParam = new ArrayList<>();
        //月总产值
        String monZczSql = "SELECT SUM(wo.real_amount) monRealCount from t_crm_car_workorder wo where from_unixtime(wo.balance_date/1000,'%Y-%m-%d') between ? and ? AND wo.company_id = ? AND wo.real_amount > '0.00'";
        //月进厂辆次
        String monCarCountSql = "SELECT COUNT(distinct concat(vin,from_unixtime(balance_date/1000,'%Y%m%d') )) monIntoCount from t_crm_car_workorder wo where from_unixtime(wo.balance_date/1000,'%Y-%m-%d') between ? and ? AND wo.company_id = ? AND wo.real_amount > '0.00' ";
        //月机电产值
        String monCarJdSql = "SELECT SUM(wo.real_amount) monJdRealCount from t_crm_car_workorder wo where from_unixtime(wo.balance_date/1000,'%Y-%m-%d') between ? and ? AND wo.wx_type != '事故车' AND wo.company_id = ? AND wo.real_amount > '0.00' ";
        //月事故产值
        String monCarSgSql = "SELECT SUM(wo.real_amount) monSgRealCount from t_crm_car_workorder wo where from_unixtime(wo.balance_date/1000,'%Y-%m-%d') between ? and ? AND wo.wx_type = '事故车' AND wo.company_id = ? AND wo.real_amount > '0.00' ";
        monParam.add(preMotnS);
        monParam.add(toDayS);
        monParam.add(companyId);


        //日总产值
        String dayZczSql = "SELECT SUM(wo.real_amount) dayRealCount from t_crm_car_workorder wo where from_unixtime(wo.balance_date/1000,'%Y-%m-%d') = ? AND wo.company_id = ? AND wo.real_amount > '0.00'";
        //日进厂辆次
        String dayCarCountSql = "SELECT COUNT(distinct wo.vin) dayIntoCount from t_crm_car_workorder wo where from_unixtime(wo.balance_date/1000,'%Y-%m-%d') = ? AND wo.company_id = ? AND wo.real_amount > '0.00' ";
        //日机电产值
        String dayCarJdSql = "SELECT SUM(wo.real_amount) dayJdRealCount from t_crm_car_workorder wo where from_unixtime(wo.balance_date/1000,'%Y-%m-%d') = ? AND wo.wx_type != '事故车' AND wo.company_id = ? AND wo.real_amount > '0.00' ";
        //日事故产值
        String dayCarSgSql = "SELECT SUM(wo.real_amount) daySgRealCount from t_crm_car_workorder wo where from_unixtime(wo.balance_date/1000,'%Y-%m-%d') = ?  AND wo.wx_type = '事故车' AND wo.company_id = ? AND wo.real_amount > '0.00' ";
        List dayParam = new ArrayList();
        dayParam.add(preDayS);
        dayParam.add(companyId);

        //月
        Map monRealMap = this.imainReportFormDao.info(monZczSql, monParam.toArray());
        Map monIntoMap = this.imainReportFormDao.info(monCarCountSql, monParam.toArray());
        Map monRealJdMap = this.imainReportFormDao.info(monCarJdSql, monParam.toArray());
        Map monRealSgMap = this.imainReportFormDao.info(monCarSgSql, monParam.toArray());
        //日
        Map dayRealMap = this.imainReportFormDao.info(dayZczSql, dayParam.toArray());
        Map dayIntoMap = this.imainReportFormDao.info(dayCarCountSql, dayParam.toArray());
        Map dayRealJdMap = this.imainReportFormDao.info(dayCarJdSql, dayParam.toArray());
        Map dayRealSgMap = this.imainReportFormDao.info(dayCarSgSql, dayParam.toArray());

        List list = new ArrayList();

        list.add(monRealMap);
        list.add(dayRealMap);
        list.add(monIntoMap);
        list.add(dayIntoMap);
        list.add(monRealJdMap);
        list.add(dayRealJdMap);
        list.add(monRealSgMap);
        list.add(dayRealSgMap);
        return list;
    }

    /**
     * @param uid 登录用户 ID
     * @return ResultEntity
     * @throws Exception
     * @desc 半年维修产值分析
     * @author 术洪超
     */
    @Override
    public Map maintainRealCount(String uid) throws Exception {

        //今天日期
        String toDayS = DateUtils.getTimeStr("yyyy-MM-dd");
        String toMontS = DateUtils.getTimeStr("yyyy-MM");
        long toDayL = DateUtils.parseTime(toDayS + " 24:00:00", "yyyy-MM-dd HH:mm:ss");
        //昨天日期
        long preDayL = DateUtils.addDayLongTime(toDayL, -2);
        String preDayS = DateUtils.formatLongTime(preDayL, "yyyy-MM-dd");
        //一个月前
        long preSixMonthL = DateUtils.addMonthLongTime(toDayL, -6);
        String preSixMonthS = DateUtils.formatLongTime(preSixMonthL, "yyyy-MM");

        String companyId = TokenUtil.getCompanyId(uid);


        List params = new ArrayList<>();
        String sql = "select from_unixtime(o.balance_date/1000,'%Y-%m') as month ,round(sum(o.real_amount),2) as realCount from t_crm_car_workorder o where o.company_id = ?  and from_unixtime(o.balance_date/1000,'%Y-%m') between ? and ? group by from_unixtime(o.balance_date/1000,'%Y-%m') ";
        params.add(companyId);
        params.add(preSixMonthS);
        params.add(toMontS);
        List<Map<String, Object>> czList = this.imainReportFormDao.list(sql, params.toArray());
        List monList = new ArrayList();
        List countList = new ArrayList();

        for (Map<String, Object> map : czList) {
            monList.add(map.get("month"));
            countList.add(map.get("realCount"));
        }

        Map total = new HashMap();
        total.put("month", monList);
        total.put("realCount", countList);

        return total;

    }

    /**
     * @param uid 登录用户 ID
     * @return ResultEntity
     * @throws Exception
     * @desc 半年进厂分析
     * @author 术洪超
     */

    @Override
    public Map intoCount(String uid) throws Exception {
        //今天日期
        String toDayS = DateUtils.getTimeStr("yyyy-MM-dd");
        String toMontS = DateUtils.getTimeStr("yyyy-MM");
        long toDayL = DateUtils.parseTime(toDayS + " 24:00:00", "yyyy-MM-dd HH:mm:ss");
        //昨天日期
        long preDayL = DateUtils.addDayLongTime(toDayL, -2);
        String preDayS = DateUtils.formatLongTime(preDayL, "yyyy-MM-dd");
        //一个月前
        long preSixMonthL = DateUtils.addMonthLongTime(toDayL, -6);
        String preSixMonthS = DateUtils.formatLongTime(preSixMonthL, "yyyy-MM");

        String companyId = TokenUtil.getCompanyId(uid);


        List params = new ArrayList<>();
        //产值>0
        String sql = "select from_unixtime(o.balance_date/1000,'%Y-%m') month ,COUNT(distinct concat(vin,from_unixtime(balance_date/1000,'%Y%m%d') ))  as intoCount from t_crm_car_workorder o where o.company_id=? and from_unixtime(o.balance_date/1000,'%Y-%m') between ? and ?  and real_amount> '0.00' group by from_unixtime(o.balance_date/1000,'%Y-%m')  ";
        params.add(companyId);
        params.add(preSixMonthS);
        params.add(toMontS);
        List<Map<String, Object>> intoList = this.imainReportFormDao.list(sql, params.toArray());
        List monList = new ArrayList();
        List countList = new ArrayList();
        for (Map map : intoList) {
            monList.add(map.get("month"));
            countList.add(map.get("intoCount"));
        }
        Map total = new HashMap();
        total.put("month", monList);
        total.put("intoCount", countList);
        return total;
    }


    /**
     * @param uid 登录用户 ID
     * @return ResultEntity
     * @throws Exception
     * @desc 查询基盘和活跃度
     * @author 术洪超
     */
    @Override
    public List base(String uid) throws Exception {

        //今天
        String toDay = DateUtils.getTimeStr("yyyy-MM-dd");
        long toDayEndLongTime = DateUtils.parseTime(toDay + " 24:00:00", "yyyy-MM-dd HH:mm:ss");
        //一个月前
        long preMonthLongTime = DateUtils.addMonthLongTime(toDayEndLongTime, -1);
        String preMonth = "";
        //一年前
        long preYearLongTime =
                DateUtils.addMonthLongTime(toDayEndLongTime, -12);
        String preYear = DateUtils.formatLongTime(preYearLongTime, "yyyy-MM-dd");

        String companyId = TokenUtil.getCompanyId(uid);
        // 基盘数
        String baseCarSQL = "select count(DISTINCT vin) as baseCount from t_crm_car c where c.series<>'QT' and c.company_id = ? AND EXISTS(SELECT 1 FROM T_CRM_CAR_WORKORDER  O WHERE O.VIN = C.VIN AND O.COMPANY_ID=C.COMPANY_ID AND O.REAL_AMOUNT > '0.00' AND FROM_UNIXTIME(O.BALANCE_DATE/1000,'%Y-%m-%d') between ? and ? )";
        List baseParams = new ArrayList();
        baseParams.add(companyId);
        baseParams.add(preYear);
        baseParams.add(toDay);
        Map baseMap = this.imainReportFormDao.info(baseCarSQL, baseParams.toArray());
        //总车辆数
        String totalCarSQL = "select count(DISTINCT vin) as totalCount from t_crm_car where company_id = ? ";
        List totalParam = new ArrayList();
        totalParam.add(companyId);
        Map totalMap = this.imainReportFormDao.info(totalCarSQL, totalParam.toArray());
        //当月新增基盘

        String newBaseSQL = "select count(1) as newBase from t_crm_car c where c.company_id=? and  Exists(select 1 from t_crm_Car_base_detail d where d.company_id = c.company_id and c.vin=d.vin and d.tag_date between ? and ? ) ";
        List newParam = new ArrayList();
        newParam.add(companyId);
        //如果今天日期为1号，取上一月数据，1号之后取当月数据

        if(toDay.equals(DateUtils.getMonthFirstDay(toDay,"yyyy-MM-dd"))){
            toDay = DateUtils.getPreMonthLastDay("yyyy-MM-dd");
            preMonth = DateUtils.getPreMonthFirstDay("yyyy-MM-dd");
        }else{
            preMonth = DateUtils.getMonthFirstDay(toDay,"yyyy-MM-dd");
        }

        newParam.add(preMonth);
        newParam.add(toDay);


        Map newMap = this.imainReportFormDao.info(newBaseSQL, newParam.toArray());
        //活跃度
        //活跃用户
        String activeCoutSQL = "SELECT count(DISTINCT temp.vin) as activeCount FROM (SELECT vin,count(DISTINCT from_unixtime(o.balance_date/1000,'%Y-%m-%d')) AS jc FROM t_crm_car_workorder o WHERE o.company_id=? AND o.real_amount> '0.00' AND from_unixtime(o.balance_date/1000,'%Y-%m-%d') BETWEEN ? AND ? GROUP BY vin) temp WHERE temp.jc> 1 ";
        List activeParams = new ArrayList();
        activeParams.add(companyId);
        activeParams.add(preYear);
        activeParams.add(toDay);
        Map activeMap = this.imainReportFormDao.info(activeCoutSQL, activeParams.toArray());
        List list = new ArrayList();
        list.add(baseMap);
        list.add(totalMap);
        list.add(newMap);
        list.add(activeMap);

        return list;
    }

}
